package com.sso.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;

@Configuration
public class redisConfig {
    @Bean
    public RedisTemplate<Object, Object> initRedisTemplate(LettuceConnectionFactory redisConnectionFactory){
        RedisTemplate<Object,Object> redisTemplate=new RedisTemplate<>();
        // redisTemplate会自动初始化StringRdisSerializer，所以在这里获取
        RedisSerializer<String> stringSerializer = redisTemplate.getStringSerializer();
        //设置字符换序列化器，这样Spring就会把redis的key当作字符串处理
        //设置String类型key设置序列器
        redisTemplate.setKeySerializer(stringSerializer);
        //为hash类型的ke设置序列齐
        redisTemplate.setHashKeySerializer(stringSerializer);
        //为hash类型value设置序列齐
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        //为String类型的value设置序列器
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return redisTemplate;
    }

}
